package com.sywb.chuangyebao.view.dialog;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.sywb.chuangyebao.R;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 提示对话框
 * Created by duxl on 2018/5/8.
 */

public class AlertDialog extends BaseDialogFragment {

    @BindView(R.id.dialog_title)
    TextView tvTitle;

    @BindView(R.id.dialog_message_text)
    TextView tvMessage;

    @BindView(R.id.dialog_button_left)
    TextView btnLeft;

    @BindView(R.id.dialog_button_line)
    View btnLine;

    @BindView(R.id.dialog_button_right)
    TextView btnRight;

    private String title;
    private String message;
    private String left;
    private String right;

    private OnClickListener mClickListener;
    private boolean mCancelable = false;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_alert;
    }

    /**
     * 创建一个AlertDialog
     *
     * @param title   对话框标题，如果为空则不显示
     * @param message 对话框详细，不能为空
     * @param left    左边按钮文字，如果为空，则显示默认文字【取消】
     * @param right   右边按钮文字，如果为空，则显示默认文字【确定】
     * @return
     */
    public static AlertDialog newInstance(String title, @NonNull String message, String left, String right, boolean isOne) {
        AlertDialog dialogFragment = new AlertDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("title", title);
        bundle.putSerializable("message", message);
        bundle.putSerializable("left", left);
        bundle.putSerializable("right", right);
        bundle.putSerializable("btn", isOne);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle == null) {
            bundle = getArguments();
        }

        title = bundle.getString("title");
        message = bundle.getString("message");
        left = bundle.getString("left");
        right = bundle.getString("right");
        boolean isOne = bundle.getBoolean("btn");

        tvTitle.setText(title);
        tvTitle.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
        tvMessage.setText(message);

        if (!TextUtils.isEmpty(left)) {
            btnLeft.setText(left);
        }

        if (!TextUtils.isEmpty(right)) {
            btnRight.setText(right);
        }

        getDialog().getWindow().setGravity(Gravity.CENTER);
        getDialog().setCancelable(mCancelable);
        getDialog().setCanceledOnTouchOutside(mCancelable);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return !mCancelable;
                }
                return false;
            }
        });

        setBtnNumber(isOne);
    }

    @Override
    public void setCancelable(boolean cancelable) {
        mCancelable = cancelable;
    }

    @OnClick({R.id.dialog_button_left, R.id.dialog_button_right})
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_button_left) {
            if (mClickListener != null) {
                mClickListener.onClick(0);
            }
            exit();

        } else if (v.getId() == R.id.dialog_button_right) {
            if (mClickListener != null) {
                mClickListener.onClick(1);
            }
            exit();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("title", title);
        outState.putString("message", message);
        outState.putString("left", left);
        outState.putString("right", right);
        super.onSaveInstanceState(outState);
    }

    public void setContextDrawable(int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        /**这一步必须要做,否则不会显示.*/
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//对图片进行压缩
        /**设置图片位置，四个参数分别方位是左上右下，都设置为null就表示不显示图片*/
        tvMessage.setCompoundDrawables(drawable, null, null, null);
    }

    public void setBtnNumber(boolean isOne) {
        btnLeft.setVisibility(View.VISIBLE);
        if (isOne) {
            btnLine.setVisibility(View.GONE);
            btnRight.setVisibility(View.GONE);
        } else {
            btnLine.setVisibility(View.VISIBLE);
            btnRight.setVisibility(View.VISIBLE);
        }
    }

    public void setClickListener(OnClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    public interface OnClickListener {
        /**
         * 按钮点击时间处理
         *
         * @param position 0左边按钮点击、1右边按钮点击
         */
        void onClick(int position);
    }
}
